// @ts-ignore
/* eslint-disable */
// import { request } from 'umi';
import { message } from 'antd';
import request from 'umi-request';
import { history } from 'umi';
// request拦截器, 改变url 或 options.
request.interceptors.response.use(async (response) => {
  if (response.status != 200) {
    if (response.status == 401) {
      history.push('/user/login');
    }
    let errMessage = "";
    try {
      errMessage = (await response.clone().json()).errMessage;
    } catch { }
    message.error("server err status:" + response.status + " errMessage:" + errMessage)
    return response;
  }
  const data = await response.clone().json();
  if (data.code != 0) {
    message.error(data.errMessage)
  }
  return response;
});

/** 获取当前的用户 GET /api/currentUser */
export async function currentUser(options?: { [key: string]: any }) {
  return request<{
    data: API.CurrentUser;
  }>('/api/currentUser', {
    method: 'GET',
    ...(options || {}),
  });
}

/** 退出登录接口 POST /api/login/outLogin */
export async function outLogin(options?: { [key: string]: any }) {
  return request<Record<string, any>>('/api/login/outLogin', {
    method: 'POST',
    ...(options || {}),
  });
}

/** 登录接口 POST /api/login/account */
export async function login(body: API.LoginParams, options?: { [key: string]: any }) {
  return request<API.LoginResult>('/api/login/account', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: body,
    ...(options || {}),
  });
}

/** 此处后端没有提供注释 GET /api/notices */
export async function getNotices(options?: { [key: string]: any }) {
  return request<API.NoticeIconList>('/api/notices', {
    method: 'GET',
    ...(options || {}),
  });
}

/** 获取规则列表 GET /api/rule */
export async function rule(
  params: {
    // query
    /** 当前的页码 */
    current?: number;
    /** 页面的容量 */
    pageSize?: number;
  },
  options?: { [key: string]: any },
) {
  return request<API.RuleList>('/api/rule', {
    method: 'GET',
    params: {
      ...params,
    },
    ...(options || {}),
  });
}

/** 新建规则 PUT /api/rule */
export async function updateRule(options?: { [key: string]: any }) {
  return request<API.RuleListItem>('/api/rule', {
    method: 'PUT',
    ...(options || {}),
  });
}

/** 新建规则 POST /api/rule */
export async function addRule(options?: { [key: string]: any }) {
  return request<API.RuleListItem>('/api/rule', {
    method: 'POST',
    ...(options || {}),
  });
}

/** 删除规则 DELETE /api/rule */
export async function removeRule(options?: { [key: string]: any }) {
  return request<Record<string, any>>('/api/rule', {
    method: 'DELETE',
    ...(options || {}),
  });
}

export async function queryUsers(options?: { [key: string]: any }) {
  return request<Record<string, any>>('/api/users', {
    method: 'GET',
    ...(options || {}),
  });
}

export async function addUser(params: any, options?: { [key: string]: any }) {
  return request<Record<string, any>>('/api/users', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: params,
    ...(options || {}),
  });
}
export async function updateUser(params: any, options?: { [key: string]: any }) {
  return request<Record<string, any>>('/api/users/' + params.id, {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
    },
    data: params,
    ...(options || {}),
  });
}

export async function deleteUser(params: any, options?: { [key: string]: any }) {
  return request<Record<string, any>>('/api/users/' + params.id, {
    method: 'DELETE',
    headers: {
      'Content-Type': 'application/json',
    },
    data: params,
    ...(options || {}),
  });
}

export async function queryContries() {
  return request<Record<string, any>>('/api/countries', {
    method: 'GET',
  });
}

export async function createCountry(params) {
  return request<Record<string, any>>('/api/countries', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: params,
  });
}

export async function updateCountry(params) {
  return request<Record<string, any>>('/api/countries/' + params.id, {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
    },
    data: params,
  });
}

export async function deleteCountry(params) {
  return request<Record<string, any>>('/api/countries/' + params.id, {
    method: 'DELETE',
  });
}


export async function queryOrders(params) {
  return request<Record<string, any>>('/api/orders', {
    method: 'GET',
    params: params,
  });
}

export async function createOrder(params) {
  return request<Record<string, any>>('/api/orders', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: params,
  });
}

export async function createDispatchOrder(params) {
  return request<Record<string, any>>('/api/dispatch-orders', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: params,
  });
}
export async function updateDispatchOrder(params) {
  return request<Record<string, any>>('/api/dispatch-orders/' + params.id, {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
    },
    data: params,
  });
}

export async function updateOrder(params) {
  return request<Record<string, any>>('/api/orders/' + params.id, {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
    },
    data: params,
  });
}

export async function deleteOrder(params) {
  return request<Record<string, any>>('/api/orders/' + params.id, {
    method: 'DELETE',
  });
}

export async function getOrderSummary(params) {
  return request<Record<string, any>>('/api/order/summary', {
    method: 'Get',
    params: params,
  });
}